//
//  SetViewController.m
//  HTHappySale
//
//  Created by 创维海通 on 2017/9/26.
//  Copyright © 2017年 zxyywy. All rights reserved.
//

#import "SetViewController.h"
#import "MainViewController.h"
#import "RegardViewController.h"
#import "WXAuth2ViewController.h"

#import "HTAssembleKey.h"
#import "BaseNavigationController.h"
#import "HTCreateWindow.h"

//Cell
#import "SetCell.h"

@interface SetViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) IBOutlet UITableView *SetTableView;

@end

@implementation SetViewController
#pragma mark -init
#define SetIdentifyCell  @"SetCell"

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    //xib
    UINib *xib = [UINib nibWithNibName:NSStringFromClass([SetCell class])
                                bundle:[NSBundle mainBundle]];
    [self.SetTableView registerNib:xib forCellReuseIdentifier:SetIdentifyCell];

    [self initTableView];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self addNavigationLeftView:@"" andType:0];
    [self addNavigationMiddleView:@"设置"];
    
    MainViewController *mainCtrol = (MainViewController *)self.tabBarController;
    [mainCtrol hiddenTabBar];
}

- (void)initTableView
{
    _SetTableView.tableFooterView = [[UIView alloc] init];
    _SetTableView.scrollEnabled = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section == 0){
        return 2;
    }
    return 1;
}

#define cellIdentify @"cell"
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentify];
    }
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            cell.textLabel.text = @"关于我们";
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }else{
            SetCell *setCell = [tableView dequeueReusableCellWithIdentifier:SetIdentifyCell];
            
            setCell.selectionStyle = UITableViewCellSelectionStyleNone;
            setCell.accessoryType = UITableViewCellAccessoryNone;
            
            return setCell;
        }
    }else if (indexPath.section == 1)
    {
        UILabel *exitLabel = [[UILabel alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - 100) / 2, 12, 100, 20)];
        exitLabel.text = @"退出登录";
        
        [cell addSubview:exitLabel];
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 15;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section
{
    return 15;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            RegardViewController *RegardVC = [[RegardViewController alloc] init];
            [self.navigationController pushViewController:RegardVC animated:YES];
        }
    }else{
        [self.navigationController popToRootViewControllerAnimated:NO];
        
        [[HTAssembleKey sharedTool] clearKey];
        NSArray *controllerArr = [self.navigationController viewControllers];
        BOOL isLogin = true;
        for (UIViewController *controller in controllerArr) {
            if ([controller isKindOfClass:[WXAuth2ViewController class]]) {
                [self.navigationController popToViewController:controller animated:YES];
                isLogin = NO;
            }
        }
        
//        if(isLogin){
//            WXAuth2ViewController *wxAuth2VC = [[WXAuth2ViewController alloc] init];
//            [self presentViewController:wxAuth2VC animated:YES completion:nil];
//            UIWindow *normalWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
            
//            UIWindow *normalWindow = [[UIApplication sharedApplication] keyWindow];
//            normalWindow.backgroundColor = [UIColor blueColor];
//            normalWindow.windowLevel = UIWindowLevelAlert;
//            WXAuth2ViewController *wxAuth2VC = [[WXAuth2ViewController alloc] init];
//            BaseNavigationController *WXAuthNac = [[BaseNavigationController alloc] initWithRootViewController:wxAuth2VC];
//            normalWindow.rootViewController = WXAuthNac;
            
//            [normalWindow makeKeyAndVisible];
            
//            UIWindow *window = [[UIApplication sharedApplication].delegate window];
//            WXAuth2ViewController *wxAuth2VC = [[WXAuth2ViewController alloc] init];
//            BaseNavigationController *WXAuthNac = [[BaseNavigationController alloc] initWithRootViewController:wxAuth2VC];
//            
//            window.rootViewController = WXAuthNac;
//            [window makeKeyAndVisible];
//        }
        
//        HTCFNotificationPost(RequestErrorCodeNotification);
        
//            UIWindow *alectWindow = [[HTCreateWindow sharedTool] createNewWindow];
//            WXAuth2ViewController *wxAuth2VC = [[WXAuth2ViewController alloc] init];
//            BaseNavigationController *WXAuthNac = [[BaseNavigationController alloc] initWithRootViewController:wxAuth2VC];
//            alectWindow.rootViewController = WXAuthNac;
//            
//            [alectWindow makeKeyAndVisible];

    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
